const time = {
    //格式化时间戳并以文本方式展示时间
      toDateText(dateTimeStamp) {
        let result = ''
        let minute = 1000 * 60; //把分，时，天，周，半个月，一个月用毫秒表示
        let hour = minute * 60;
        let day = hour * 24;
        let week = day * 7;
        let halfamonth = day * 15;
        let month = day * 30;
        let now = new Date().getTime(); //获取当前时间毫秒
        let diffValue = now - dateTimeStamp; //时间差
        if (diffValue < 0) return "刚刚"
        let minC = diffValue / minute; //计算时间差的分，时，天，周，月
        let hourC = diffValue / hour;
        let dayC = diffValue / day;
        let weekC = diffValue / week;
        let monthC = diffValue / month;
        if (monthC >= 1 && monthC <= 3) {
          result = " " + parseInt(monthC) + "月前"
        } else if (weekC >= 1 && weekC <= 3) {
          result = " " + parseInt(weekC) + "周前"
        } else if (dayC >= 1 && dayC <= 6) {
          result = " " + parseInt(dayC) + "天前"
        } else if (hourC >= 1 && hourC <= 23) {
          result = " " + parseInt(hourC) + "小时前"
        } else if (minC >= 1 && minC <= 59) {
          result = " " + parseInt(minC) + "分钟前"
        } else if (diffValue >= 0 && diffValue <= minute) {
          result = "刚刚"
        } else {
          let datetime = new Date();
          datetime.setTime(dateTimeStamp);
          let Nyear = datetime.getFullYear();
          let Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
          let Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
          let Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
          let Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
          let Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
          result = Nyear + "-" + Nmonth + "-" + Ndate
        }
        return result;
      },

      //获取时间戳，并格式化
      getDate(format, strInterval, number) {
        var myDate = new Date();
        var dtTmp = new Date();
        if (!!strInterval) {
          switch (strInterval) {
            case 's':
              myDate = new Date(Date.parse(dtTmp) + (1000 * number)); // 秒
              break;
            case 'n':
              myDate = new Date(Date.parse(dtTmp) + (60000 * number)); // 分
              break;
            case 'h':
              myDate = new Date(Date.parse(dtTmp) + (3600000 * number)); // 小时
              break;
            case 'd':
              myDate = new Date(Date.parse(dtTmp) + (86400000 * number)); // 天
              break;
            case 'w':
              myDate = new Date(Date.parse(dtTmp) + ((86400000 * 7) * number)); // 星期
              break;
            case 'q':
              myDate = new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); // 季度
              break;
            case 'm':
              myDate = new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); // 月
              break;
            case 'y':
              myDate = new Date((dtTmp.getFullYear() + number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); // 年
              break;
            default:
          }
        }
        return time.toDate(myDate, format);
      },

      //将时间戳格式化成想要的格式，默认为 "yyyy-MM-dd HH:mm" - "2022-09-06 14:07"
      toDate(v, format) {
        format = format ? format : "yyyy-MM-dd HH:mm"
        if (!v) return "";
        var d = v;
        if (typeof v === 'string') {
          if (v.indexOf("/Date(") > -1)
            d = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
          else
            d = new Date(Date.parse(v.replace(/-/g, "/").replace("T", " ").split(".")[0]));
        } else {
          d = new Date(v)
        }
        var o = {
          "M+": d.getMonth() + 1,
          "d+": d.getDate(),
          "h+": d.getHours(),
          "H+": d.getHours(),
          "m+": d.getMinutes(),
          "s+": d.getSeconds(),
          "q+": Math.floor((d.getMonth() + 3) / 3),
          "S": d.getMilliseconds()
        };
        if (/(y+)/.test(format)) {
          format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
          if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
          }
        }
        return format;
      }
}

export default time